<?php

// 本类由系统自动生成，仅供测试用途
class MessageAction extends CommonAction
{

    public function material()
    {

        $this->display();
    }

    /**
     * 查询图文消息列表
     */
    public function textmessages()
    {
        $profile_id = $_SESSION['profile_id'];
        $db = D("Graphic");
        $this->graphicList = $db->relation(true)->where("create_user_id =%d", $profile_id)->select();
        $this->display();
    }

    /**
     * 单图文消息编辑
     */
    public function oneTuWen()
    {
        $id = I("id", 0);
        if ($id > 0) {
            $this->article = M("article")->where(" id=" . $id)->find();
        }
        $this->display();
    }

    /**
     *
     */
    public function saveArticle()
    {
        $id = I("id", 0);
        if (0 == $id) {
            $article = D("article");
            $article->create();
            $graphic = M('Graphic');
            $graphic->create_user_id = 19;//暂时写死
            $graphic->create_time = date("Y-m-d H:i:s");//暂时
            $graphic->oneOrMore = 1;
            $graphic->startTrans();
            $ok1 = $graphic->add();
            $ok2 = 0;
            if ($ok1 > 0) {
                $article->graphic_id = $ok1;
                $ok2 = $article->add();
            }
            if ($ok2 > 0) {
                $graphic->commit();
                outSuccess();
            } else {
                $graphic->rollback();
                outError("提交失败");
            }
        } else {
            $article = D("article");
            $article->create();
            $ok1 = $article->save();
            outSuccess();
        }
    }

    /**
     *
     */
    public function delete()
    {
        $id = I("id", 0);
        if ($id > 0) {
            $db = M("article");
            $article = $db->where(" id=%d", $id)->find();
            if (isset($article["graphic_id"])) {
                $db->startTrans();
                M("Graphic")->where(" id=%d ", $article["graphic_id"])->delete();
                $db->where(" id=%d ", $id)->delete();
                $db->commit();
                outSuccess();
            } else {
                $db->where(" id=%d ", $id)->delete();
                outSuccess();
            }
        } else {
            outError("error");
        }
    }

    public function deleteMoreTuWen()
    {
        $id=I("id", 0);
        if ($id > 0) {
            $db = M("article");
            $db->startTrans();
            $db->where(" graphic_id=%d", $id)->delete();
            M("graphic")->where(" id=%d ", $id)->delete();
            $db->commit();
            outSuccess();
        } else {
            outError("error");
        }
    }

    public function moreTuWen()
    {
        $id = I("id", 0);
        if ($id > 0) {
            $this->graphic = M("graphic")->where("id=%d", $id)->find();
            $articleList = M("article")->where("graphic_id =%d", $id)->select();
            $this->article = $articleList[0];
            unset($articleList[0]);
            $this->articleList = $articleList;
        } else {
            $article = array();
            $article["type"] = "link";
            $this->article = $article;
        }
        $this->display();
    }

    public function saveDuotuwen()
    {
        $graphicId = I("graphicId", 0);
        $headTitles = I('headTitle');// I("headTitle");//标题
        $authors = I("author");//作者
        $mainTexts = I("mainText");//正文
        $originalLinks = I("originalLink");//原文链接
        $types = I("type");//类型
        $linkUrls = I("linkUrl");
        $imgIds = I("imgId");//图片 id
        $ok2 = 0;
        if (0 == $graphicId) {
            $graphic = M('Graphic');
            $graphic->create_user_id = 19;//暂时写死
            $graphic->create_time = date("Y-m-d H:i:s");//暂时
            $graphic->oneOrMore = 2;
            $graphic->startTrans();
            $ok1 = $graphic->add();
            if ($ok1 > 0) {
                if (is_array($headTitles) && is_array($authors) && is_array($mainTexts) && is_array($originalLinks) && is_array($types) && is_array($linkUrls) && is_array($imgIds)) {
                    for ($i = 0; $i < count($headTitles); $i++) {
                        $article = D("article");
                        $article->graphic_id = $ok1;
                        $article->head_title = $headTitles[$i];
                        $article->author = $authors[$i];
                        $article->type = $types[$i];
                        $article->mainText = $mainTexts[$i];
                        $article->originalLink = $originalLinks[$i];
                        $article->linkUrl = $linkUrls[$i];
                        $article->img_id = $imgIds[$i];
                        $ok2 = $article->add();
                        if ($ok2 <= 0) {
                            break;
                        }
                    }
                }
            }
            if ($ok2 > 0) {
                $graphic->commit();
                outSuccess();
            } else {
                $graphic->rollback();
                outError("提交失败");
            }
        } else {
            $arts = M('article');
            $arts->startTrans();
            $arts->where(" graphic_id=%d ", $graphicId)->delete();
            if (is_array($headTitles) && is_array($authors) && is_array($mainTexts) && is_array($originalLinks) && is_array($types) && is_array($linkUrls) && is_array($imgIds)) {
                for ($i = 0; $i < count($headTitles); $i++) {
                    $article = M("article");
                    $article->graphic_id = $graphicId;
                    $article->head_title = $headTitles[$i];
                    $article->author = $authors[$i];
                    $article->type = $types[$i];
                    $article->mainText = $mainTexts[$i];
                    $article->originalLink = $originalLinks[$i];
                    $article->linkUrl = $linkUrls[$i];
                    $article->img_id = $imgIds[$i];
                    $ok2 = $article->add();
                    if ($ok2 <= 0) {
                        break;
                    }
                }
            }
            if ($ok2 > 0) {
                $arts->commit();
                outSuccess();
            } else {
                $arts->rollback();
                outError("提交失败");
            }
        }
    }
}